<?php

class AuthController extends BaseController{
    /**
     * 页面：登录
     * @return Response
     */
    public function getLogin()
    {

        return View::make('auth.login');
    }
    /**
     * 动作：登录
     * @return Response
     */
    public function postLogin()
    {
        //这里的key必须叫password,如果你的数据库里密码字段不叫password,
        //请修改app/models/Use.php 中的 getAuthPassword 方法，返回您的字段名即可
        $user = array(
                'user_login' => Input::get('username'),
                'user_pass'   => Input::get('password'),
                );
        $remember    = Input::get('remember-me', 0);

        if (Auth::viaRemember() || Auth::attempt($user, $remember)) {
            // 登录成功，跳回之前被拦截的页面
            return Response::json(array(
                                  'login_status' => 'success',
                                  'redirect_url' => URL::previous(),
                                 ));
        } else {
            return Response::json(array(
                                  'login_status' => 'invalid',
                                 ));
        }
    }
    public function getRegister()
    {
        return View::make('auth.register');
    }

    public function postRegister()
    {
        $user = new User;
        $user->user_login    = Input::get('user_nicename');
        $user->user_email    = Input::get('user_email');
        $user->user_pass     = Hash::make(Input::get('user_pass'));
        $user->user_nicename = Input::get('user_nicename');
        if ($user->save()) {
            // 添加成功
            // 生成激活码
            // $activation = new Activation;
            // $activation->email = $user->email;
            // $activation->token = str_random(40);
            // $activation->save();
            // // 发送激活邮件
            // $with = array('activationCode' => $activation->token);
            // Mail::send('authority.email.activation', $with, function ($message) use ($user) {
            //     $message
            //         ->to($user->email)
            //         ->subject('wuwenhan 账号激活邮件'); // 标题
            // });
            // 跳转到注册成功页面，提示用户激活
            return Response::json();
        } else {
            // 添加失败
            return Redirect::back()
                ->withInput()
                ->withErrors(array('add' => '注册失败。'));
        }
    }

}
